﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Server
{
    public partial class Form1 : Form
    {
        Thread t1,t2,t3;
        int n = 0;
        SocKet soc = new SocKet();
        public Form1()
        {
            InitializeComponent();
        }

        private void Open_Click(object sender, EventArgs e)
        {
            soc.Op();
            n = soc.n;
            for (int i = 0; i < n; i++)
            {
                // string tem = soc.client(i);
                lstIP.Items.Add(soc.client(i).ToString());
            }
            //CapNhat();
           
         //   t1 = new Thread(new ThreadStart(client));
          //  t1.Start();

            t2 = new Thread(new ThreadStart(Chat_client0));
            t2.Start();

            t3 = new Thread(new ThreadStart(Chat_client1));
            t3.Start();
        }

        public void Chat_client0()
        {
            string st, s;
            while (true)
            {
                
                st = soc.Receive(0);
                if (st == "1")// client1 danh co;
                {
                    s = soc.Receive(0);
                    soc.send(1,"1");// gui nuoc danh
                    soc.send(1, s);
                }
                if (st == "3")
                {
                    s = soc.Receive(0);
                    s = soc.NameCL[0] + ": " + s;
                    lstChat.Items.Add(s);
                    soc.send(1, "3");// gui chat
                    soc.send(1, s);
                }
                if (st=="4")
                {
                    soc.send(1, "4");// 3 thoat
                    break;
                }
            }
        
        }

        public void Chat_client1()
        {
            string st, s;
            while (true)
            {

                st = soc.Receive(1);
                if (st == "1")// client1 danh co;
                {
                    s = soc.Receive(1);
                    soc.send(0, "1");// gui nuoc danh
                    soc.send(0, s);
                }
                if (st == "3")// nhan chat
                {
                    
                    s = soc.Receive(1);
                    s = soc.NameCL[1] + ": " + s;
                    lstChat.Items.Add(s);
                    soc.send(0, "3");// gui chat
                    soc.send(0, s);
                }
                if (st == "4")
                {
                    soc.send(0, "4");// 3 thoat
                    break;
                }
            }
        }

       

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void close_Click(object sender, EventArgs e)
        {
            soc.send(0, "4");
            soc.send(1, "4");
            t1.Abort();
            t2.Abort();
            t3.Abort();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
